package cn.itcast.demo.mq.work;

import cn.itcast.demo.mq.utils.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import static cn.itcast.demo.mq.work.ReceiveDemo1.QUEUE_NAME;

/**
 * 发送者
 *
 * @author 虎哥
 */
public class SendDemo {
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        // 建立连接
        Connection connection = ConnectionUtils.getConnection();
        // 获取通道Channel
        Channel channel = connection.createChannel();
        // 创建队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        // 发送消息
        for (int i = 0; i < 50; i++) {
            String message = "message_" + i;
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
            System.out.println("消息已经发送： " + message);
            Thread.sleep(50);
        }

        // 关闭连接和通道
        channel.close();
        connection.close();
    }
}
